/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx.controller;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>Project: smvc2022 - IndexController
 * <p>Powered by webrx On 2022-03-30 16:52:34
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
@Controller
//@CrossOrigin(origins = "*", methods = {RequestMethod.GET, RequestMethod.DELETE, RequestMethod.PATCH, RequestMethod.POST, RequestMethod.PUT}, maxAge = 3600)
public class IndexController {
    @RequestMapping
    public String index(){
        return "home";
    }

    @Data
    static class User{
        int id;
        String name;
    }
    @GetMapping("/api/show") @ResponseBody @CrossOrigin
    public Map<String,Object> show(User user, HttpServletRequest req) throws IOException {
        System.out.println(user);
        System.out.println(req.getMethod());
        Map<String,Object> map = new HashMap<>();
        map.put("code",200);
        map.put("method",req.getMethod());
        map.put("ip",req.getRemoteAddr());
        map.put("msg","提交数据成功");
        map.put("data",user);
        return map;
    }

    @PostMapping("/api/pp") @ResponseBody @CrossOrigin
    public Map<String,Object> pp(@RequestBody User user, HttpServletRequest req) throws IOException {
        System.out.println(user);
        System.out.println(req.getMethod());
        Map<String,Object> map = new HashMap<>();
        map.put("code",200);
        map.put("method",req.getMethod());
        map.put("ip",req.getRemoteAddr());
        map.put("msg","axios post 提交数据成功");
        map.put("data",user);
        return map;
    }

    @PostMapping("/api/pp2") @ResponseBody @CrossOrigin
    public Map<String,Object> pp2(User user,Integer age, HttpServletRequest req) throws IOException {
        System.out.println(user);
        System.out.println(req.getMethod());
        Map<String,Object> map = new HashMap<>();
        map.put("code",200);
        map.put("method",req.getMethod());
        map.put("ip",req.getRemoteAddr());
        map.put("msg","axios post 提交数据成功");
        map.put("data",user);
        map.put("age",age);
        return map;
    }

    @PutMapping("/api/pt") @ResponseBody @CrossOrigin
    public Map<String,Object> pt(@RequestBody User user,Integer age, HttpServletRequest req) throws IOException {
        System.out.println(user);
        System.out.println(req.getMethod());
        Map<String,Object> map = new HashMap<>();
        map.put("code",200);
        map.put("method",req.getMethod());
        map.put("ip",req.getRemoteAddr());
        map.put("msg","axios put 提交数据成功");
        map.put("data",user);
        map.put("age",age);
        return map;
    }

    @DeleteMapping("/api/del") @ResponseBody @CrossOrigin
    public Map<String,Object> del(User user,Integer age, HttpServletRequest req) throws IOException {
        System.out.println(user);
        System.out.println(req.getMethod());
        Map<String,Object> map = new HashMap<>();
        map.put("code",200);
        map.put("method",req.getMethod());
        map.put("ip",req.getRemoteAddr());
        map.put("msg","axios put 提交数据成功");
        map.put("data",user);
        map.put("age",age);
        return map;
    }

    @PatchMapping("/api/patch") @ResponseBody @CrossOrigin
    public Map<String,Object> patch(@RequestBody User user,Integer age, HttpServletRequest req) throws IOException {
        System.out.println(user);
        System.out.println(req.getMethod());
        Map<String,Object> map = new HashMap<>();
        map.put("code",200);
        map.put("method",req.getMethod());
        map.put("ip",req.getRemoteAddr());
        map.put("msg","axios put 提交数据成功");
        map.put("data",user);
        map.put("age",age);
        return map;
    }


    /**
     * Description: 自己处理的字符串，fetch ajax res.text()
     * @date: 2022/3/30 17:02
     * @params: []
     * @return: java.lang.String
     */
    @GetMapping("/timer") @ResponseBody
    public String timer(){
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }


    /**
     * Description: 返回的json字符串 所以  res.text()  有双引号 res.json()
     * @date: 2022/3/30 17:01
     * @params: []
     * @return: java.time.LocalDateTime
     */
    @GetMapping("/timer2") @ResponseBody
    public LocalDateTime timer2(){
        return LocalDateTime.now();
    }
}
